package com.astn.bks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.astn.adapter.booking.PBedAdapter;
import com.astn.adapter.booking.PFacilityAdapter;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.JsonUtils;
import com.astn.entity.booking.QuoteBed;
import com.astn.entity.booking.QuoteFacility;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class COfferView extends Activity {

	private RatingBar ratStar;
	private TextView txvPriceVal;
	private TextView txvRoomTypeVal;
	private ListView lsvBeds;
	private ListView lsvFacilities;
	private Button btnBookThis;
	private Button btnCancel;
	private JsonObject currentOffer;
	private ArrayList<QuoteBed> lstBeds;
	private ArrayList<QuoteFacility> lstFacilities;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater lInflater = LayoutInflater.from(this);
		View mainLayout = lInflater.inflate(R.layout.cviewoffer, null);
		setContentView(R.layout.cviewoffer);
		String jsonData = getIntent().getStringExtra(BksConstants.EXTRA_OFFER);
		currentOffer = (JsonObject) JsonUtils.parser.parse(jsonData);
		if (currentOffer == null) {
			Toast.makeText(this, "Can not read offer infomation.",
					Toast.LENGTH_SHORT).show();
			setResult(BksConstants.RESULT_ERROR);
			finish();
		}
		ratStar = (RatingBar) findViewById(R.id.ratQuality);
		txvPriceVal = (TextView) findViewById(R.id.txvPriceVal);
		txvRoomTypeVal = (TextView) findViewById(R.id.txvRoomTypeVal);
		lsvBeds = (ListView) findViewById(R.id.lsvBeds);
		lsvFacilities = (ListView) findViewById(R.id.lsvFacilities);
		btnBookThis = (Button) findViewById(R.id.btnBook);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		ratStar.setRating(currentOffer.get("star").getAsFloat());
		txvPriceVal.setText(currentOffer.get("price").getAsString());
		txvRoomTypeVal.setText(currentOffer.get("roomtype").getAsJsonObject()
				.get("roomtypename").getAsString());
		lstBeds = new ArrayList<QuoteBed>();
		String bedsStr = currentOffer.get("beds").toString();
		Map<String, Object> mapBeds = null;
		try {
			mapBeds = JsonUtils.gson.fromJson(bedsStr, HashMap.class);
		} catch (Exception e) {
			Toast.makeText(this, "Data structure error", Toast.LENGTH_SHORT)
					.show();
			setResult(BksConstants.RESULT_ERROR);
			finish();
		}
		for (String s : mapBeds.keySet()) {
			lstBeds.add(new QuoteBed(s, ((Double) mapBeds.get(s)).intValue()));
		}
		JsonArray jarFacilities = null;
		try {
			jarFacilities = currentOffer.get("facilities").getAsJsonArray();
		} catch (Exception e) {
			Toast.makeText(this, "Data structure error", Toast.LENGTH_SHORT)
					.show();
			setResult(BksConstants.RESULT_ERROR);
			finish();
		}
		lstFacilities = new ArrayList<QuoteFacility>();
		for (int i = 0; i < jarFacilities.size(); i++) {
			lstFacilities
					.add(new QuoteFacility(jarFacilities.get(i)
							.getAsJsonObject().get("id").getAsInt(),
							jarFacilities.get(i).getAsJsonObject()
									.get("facilityname").getAsString()));
		}
		PBedAdapter bedAdapter = new PBedAdapter(this, lstBeds);
		lsvBeds.setAdapter(bedAdapter);
		PFacilityAdapter faAdapter = new PFacilityAdapter(this, lstFacilities);
		lsvFacilities.setAdapter(faAdapter);

	}

}
